package com.vector.rabbitmqDemo.confirm.async.recv;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * @program: rabbitmqdemo
 * @description: 确认模式-异步-消息消费者队列
 * @author: Acerola
 * @create: 2021-09-12 21:31
 **/
public class Recv {
    public static final String QUEUE_NAME = "confirm_async";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("39.99.141.194");
        factory.setUsername("yeb");
        factory.setVirtualHost("/yeb");
        factory.setPassword("yeb");
        factory.setPort(5672);
        // 获取连接
        Connection connetion = factory.newConnection();

        // 创建通道
        Channel channel = connetion.createChannel();

        // 声明队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 接受数据，并监听
        channel.basicConsume(QUEUE_NAME, new DefaultConsumer(channel) {
            public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
                    throws IOException {

                System.out.println("confirm Revc：" + new String(body, "utf-8"));

            }
        });

    }

}
